package com.think.teaching.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.think.teaching.model.Chapter;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 章 Mapper 接口
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
public interface ChapterMapper extends BaseMapper<Chapter> {
	
	@Select("<script>"
			+ "SELECT IFNULL(MAX(`index`),0)  as `index` FROM (SELECT *  FROM chapter WHERE course_id = #{course} "
			+ "<if test='parent gt 0'>AND parent=#{parent}</if>"
			+ "<if test='parent lt 1'>AND ISNULL(parent)</if>"
			+ ") a"
			+ "</script>")
	Integer getMaxIndexOfCurrentChapterLayer(@Param("course") Integer course,@Param("parent") Integer parent);


	@Select("SELECT * FROM chapter WHERE id=#{id} AND delflag = 0")
	@Results({
		@Result(column = "id",property = "id"),
		@Result(column = "{id=id,lessonType=lesson_type}",property =
				"lessonDetails",
				one = @One(select = "com.think.teaching.mapper.ChapterMapper.getLessonDetails",fetchType = FetchType.EAGER))
	})
	
	Chapter getChapterDetails(@Param("id")Integer id);
	
	@Select("<script>" +
			"SELECT course.id as course_id,course_type.type_name,r.* FROM course LEFT JOIN course_type ON course.type=course_type.id RIGHT JOIN (SELECT v.*,chapter.course_id FROM " +
			"<if test='lessonType==1'>article_lesson</if>"
			+
			"<if test='lessonType==2'>video_lesson</if>"
			+
			"<if test='lessonType==3'>vr_lesson</if>"
			+
			"<if test='lessonType==4'>living_lesson</if>"
			+
			" v,chapter WHERE chapter_id=#{id} AND chapter_id = chapter.id) r ON r.course_id=course.id;"
			+
			"</script>"
	)
	Map<String, Object> getLessonDetails(@Param("id") Integer id,@Param("lessonType") int lessonType);
	
	@Select("<script>" +
			"SELECT * FROM" +
			"<if test='lessonType==1'>article_lesson</if>"
			+
			"<if test='lessonType==2'>video_lesson</if>"
			+
			"<if test='lessonType==3'>vr_lesson</if>"
			+
			"<if test='lessonType==4'>living_lesson</if>"
			+
			" WHERE chapter_id=#{id} AND delflag=0"
			+
			"</script>"
	)
	Map<String, Object> getLessonDetails1(Map<String, Object> map);
	
	@Delete("<script>" +
			"DELETE FROM" +
			"<if test='lessonType==1'>article_lesson</if>"
			+
			"<if test='lessonType==2'>video_lesson</if>"
			+
			"<if test='lessonType==3'>vr_lesson</if>"
			+
			"<if test='lessonType==4'>living_lesson</if>"
			+
			" WHERE chapter_id=#{id};"
			+
			"</script>"
	)
	int DeleteLesson(@Param("id") Integer id,@Param("lessonType") Integer lessonType);
	
	@Update("<script>" +
			"UPDATE " +
			"<if test='lessonType==1'>article_lesson</if>"
			+
			"<if test='lessonType==2'>video_lesson</if>"
			+
			"<if test='lessonType==3'>vr_lesson</if>"
			+
			"<if test='lessonType==4'>living_lesson</if>"
			+
			"SET state=#{state}"
			+
			" WHERE chapter_id=#{id} AND delflag=0"
			+
			"</script>"
	)
	int ChangeLessonState(@Param("id") Integer id,
						  @Param("lessonType") Integer lessonType,
						  @Param("state") Integer state);
	
//	@Select("SELECT * FROM chapter WHERE id=#{id} AND delflag = 0 AND parent=`NULL`")
//	@Results({
//		@Result(column = "id",property = "id"),
//		@Result(column = "id",property = "lessonDeatiles",many = Many(select = "com.think.teaching.mapper.ChapterMapper.getLessonDetailes",fetchType = FetchType.EAGER))
//	})
	@Select("SELECT * FROM chapter WHERE id=#{id} AND delflag = 0")
	List<Chapter> getChaptersByCourseId(@Param("id")Integer id);
	
	
}
